const app = getApp()
Page({
  data: {
    spinShow: false,
    logoutModalVisible: false,
    count: 0,
    canvasWidth: "",
    canvasHeight: "",
    canvasLeft: "",
    canvasTop: "",
    companyName:'保密',//公司名称
    position: '你猜',//部门
    station: '高级人才',
    employeeName:'不告诉你',
    showhaibao: false, //隐藏显示
    maskHidden: true, //隐藏显示
    is_admin: null,//管理员权限
    thcl: '', //VIP账号
    identitycard:'' ,//身份证号
    showModal: false,
    modalName: null,
    inputidentitycard:'',
    iconList: [{
      name: "refresh",
      size: 26
    }, {
      name: "search",
      size: 26
    }, {
      name: "ios",
      size: 26
    }],
    dataList: [{
      name: "refresh",
      size: 30
    }, {
      name: "search",
      size: 30
    }, {
      name: "close-fill",
      size: 30
    }]
  },

  onLoad: function (options) {
    this.setData({
      spinShow: true,
    });
    this.loadUserInfo()
    this.setData({
      spinShow: false
    });
    var value = wx.getStorageSync('empinfo')
    //console.log(value)
        if(value){
            //console.log('管理员手机号'+value.adminMobile)
            this.setData({
                companyName: value.companyName,
                employeeName:value.employeeName,
                position:value.position,
                station:value.station,
                identitycard:value.identitycard,
                is_admin: value.adminMobile,
                thcl: value.mobile
              })
        }
  },
  onReady: function () {

    this.create();
    //创建初始化图片

  },
  hideModal: function () {
    this.setData({
      logoutModalVisible: false
    })
  },
  onShow: function () {
    this.setData({
      maskHidden: true,
      showhaibao: false
    })
  },
  loadUserInfo() {
    let _this = this
    _this.setData({
      spinShow: true
    });
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  logOut() {
    let _this = this
    _this.setData({
      spinShow: true
    });
        wx.setStorageSync('token', '')
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      _this.setData({
        spinShow: false
      });
  },
  goAuthentication(){
    this.setData({
      modalName: 'DialogModal1'
    });
  },
  authentication(){
    if(this.inputidentitycard==null){
      wx.showModal({
        title: '请输入您的身份证号！'
      })
      return
    }
    var value = wx.getStorageSync('empinfo')
    app.formPost('/wx/user/authentication', {
      'inputidentitycard': this.inputidentitycard,
      'employeeId': value.employeeId
      }).then(res =>{
        if(res.code == 200){
          console.log(res)
          //wx.setStorageSync('empinfo', '')
          wx.setStorageSync('empinfo', res.newempinfo)
          //var empinfo = wx.getStorageSync('empinfo')
          //console.log("555",empinfo)
          this.setData({
            modalName: null
          })
          this.onLoad();
          wx.showToast({
            title: '认证成功!',
            icon: 'success',
            duration: 2000
          })
        }else if(res.code == 201){
          console.log(res)
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 2000
          })
        }
      })
    //console.log( "222",this.inputidentitycard)
  },
  syncInputValue(e){
    let that=this
    that.inputidentitycard=e.detail.value
    console.log( that.inputidentitycard)
  },
  showMask: function (e) {
    this.selectComponent("#authorCardId").showMask()
  },
  clickHandler() {
    this.setData({
      count: this.data.count + 1
    });
  },
  clickIng(e) {
    let that = this
    this.zan = setInterval(function () {
      that.setData({
        count: that.data.count + 1
      });
    }, 100)
  },
  clickCancel() {
    clearInterval(this.zan)
  },
  // 震动
  vibrateLongTap: function () {
    // 使手机振动400ms
    wx.vibrateLong();
  },
  vibrateShortTap: function () {
    // 使手机振动15ms
    wx.vibrateShort();
  },
  share: function (e) {
    $Toast({
      content: '图片生成中...',
      type: 'loading',
      duration: 0
    });
    var _this = this
    setTimeout(function () {
      _this.create();
      _this.setData({
        maskHidden: false,
        showhaibao: true
      })
      $Toast.hide();
    }, 2000)
  },
  //创建
  getImage: function (url) {
    return new Promise((resolve, reject) => {
      wx.getImageInfo({
        src: url,
        success: function (res) {
          resolve(res)
        },
        fail: function () {
          reject("")
        }
      })
    })
  },
  getImageAll: function (image_src) {
    let that = this;
    var all = [];
    image_src.map(function (item) {
      all.push(that.getImage(item))
    })
    return Promise.all(all)
  },
  //创建
  create: function () {
    let that = this;
    //图片一把是通过接口请求后台，返回俩点地址，或者网络图片
    // let bg = 'https://www.qinxuewu.club/upload/2018/11/170644l325qooyabhioyaa20181224181300736.jpg';
    // let qr = 'http://www.qinxuewu.club/upload/2018/11/gh_389b881555ad_25820181225234435110.jpg';
    let bg = '../../../images/logo.jpg';
    let qr = '../../../images/gongzhang.png';
    //图片区别下载完成，生成临时路径后，在尽心绘制
    this.getImageAll([bg, qr]).then((res) => {
      let bg = res[0];
      let qr = res[1];
      //设置canvas width height position-left,  为图片宽高
      this.setData({
        canvasWidth: bg.width + 'px',
        canvasHeight: bg.height + 'px',
        canvasLeft: `-${bg.width + 100}px`,
      })
      let ctx = wx.createCanvasContext('canvas');
      ctx.drawImage(bg.path, 0, 0, bg.width, bg.height);
      ctx.drawImage(qr.path, bg.width - qr.width - 100, bg.height - qr.height - 150, qr.width * 0.8, qr.height * 0.8)
      ctx.setFontSize(20)
      ctx.setFillStyle('black')
      ctx.setTextAlign('center')
      ctx.fillText('一款简单的小程序博客', bg.width - qr.width - 1, bg.height - qr.height - 190)
      ctx.draw(that.save());

      // wx.showModal({
      //   title: '提示',
      //   content: '图片绘制完成',
      //   showCancel: false,
      //   confirmText: "点击保存",
      //   success: function () {
      //     that.save()
      //   }
      // })


    })
  },
  //保存
  save: function () {
    var that = this;
    //canvas 生成图片 生成临时路径
    wx.canvasToTempFilePath({
      canvasId: 'canvas',
      success: function (res) {
        //console.log(res.tempFilePath);
        var tempFilePath = res.tempFilePath;
        that.setData({
          imagePath: tempFilePath
        });

      }
    })
  },
  gotoSubmit: function (e) {
    $Toast({
      content: '图片生成中...',
      type: 'loading',
      duration: 0
    });
    var _this = this
    setTimeout(function () {
      _this.create();
      _this.setData({
        maskHidden: false,
        showhaibao: true
      })
      $Toast.hide();
    }, 2000)

  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  savelocalImg: function () {
    var _this = this
    wx.saveImageToPhotosAlbum({
      //下载图片
      filePath: _this.data.imagePath,
      success: function () {
        wx.showToast({
          title: "保存成功",
          icon: "success",
        })
        _this.setData({
          maskHidden: false,
          showhaibao: true
        })
      }
    });
  },
  onPullDownRefresh() {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    setTimeout(function () {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      //停止当前页面下拉刷新。
      wx.stopPullDownRefresh()
    }, 1000)
  }
})